package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type Message struct {
	Id        int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId int64     `gorm:"column:student_id"`
	Message   string    `gorm:"column:message"`
	IsRead    bool      `gorm:"column:is_read"`
	CreatedAt time.Time `gorm:"column:created_at"`
}

func (*Message) TableName() string {
	return "message"
}

type Message0 struct {
	StudentId int64  `gorm:"column:student_id"`
	Message   string `gorm:"column:message"`
	IsRead    bool   `gorm:"column:is_read"`
}

func (*Message0) TableName() string {
	return "message"
}

func (m *Message0) Insert() (err error) {
	err = nspel.GetOrm().Create(m).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("message.student_id:"+strconv.FormatInt(m.StudentId, 10)+".count", "message1.student_id:"+strconv.FormatInt(m.StudentId, 10)+".all", "message.student_id:"+strconv.FormatInt(m.StudentId, 10)+".is_read:"+strconv.FormatBool(false)+".count")
	return
}

type Message1 struct {
	Id        int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	Message   string    `gorm:"column:message"`
	IsRead    bool      `gorm:"column:is_read"`
	CreatedAt time.Time `gorm:"column:created_at"`
}

func (*Message1) TableName() string {
	return "message"
}

type MessageE struct{}

func (*MessageE) TableName() string {
	return "message"
}

func (m *MessageE) SelectCountByStudentId(studentId int64) (count int64, err error) {
	key := "message.student_id:" + strconv.FormatInt(studentId, 10) + ".count"
	err = nspel.RedisGetInt64(key, &count)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Table(m.TableName()).Where("student_id = ?", studentId).Count(&count).Error; err != nil {
		return
	}
	nspel.RedisSetInt64(key, count)
	return
}

func (m *MessageE) SelectCountByStudentIdIsRead(studentId int64, isRead bool) (count int64, err error) {
	key := "message.student_id:" + strconv.FormatInt(studentId, 10) + ".is_read:" + strconv.FormatBool(isRead) + ".count"
	err = nspel.RedisGetInt64(key, &count)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Table(m.TableName()).Where("student_id = ?", studentId).Where("is_read = ?", isRead).Count(&count).Error; err != nil {
		return
	}
	nspel.RedisSetInt64(key, count)
	return
}

func (m *MessageE) SelectAllByStudentId(studentId int64) (messages []Message1, err error) {
	key := "message1.student_id:" + strconv.FormatInt(studentId, 10) + ".all"
	err = nspel.RedisGetGob(key, &messages)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "message", "is_read", "created_at"}).Where("student_id = ?", studentId).Find(&messages).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, messages)
	return
}

func (m *MessageE) UpdateIsReadByIdStudentIdIsRead(id int64, studentId int64, isRead bool) (err error) {
	if err = nspel.GetOrm().Table(m.TableName()).Where("id = ?", id).Where("student_id = ?", studentId).Where("is_read = ?", false).Update("is_read", true).Error; err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("message.student_id:"+strconv.FormatInt(studentId, 10)+".count", "message1.student_id:"+strconv.FormatInt(studentId, 10)+".all", "message.student_id:"+strconv.FormatInt(studentId, 10)+".is_read:"+strconv.FormatBool(false)+".count")
	return
}
